package com.specialized.driver.service.impl;

import java.util.List;

import com.specialized.common.core.domain.Result;
import com.specialized.orders.common.domain.RealTime;
import com.specialized.orders.RealTimeOrderRmote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import com.specialized.driver.mapper.DriverMapper;
import com.specialized.driver.domain.Driver;
import com.specialized.driver.service.IDriverService;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 司机Service业务层处理
 *
 * @author djh
 * @date 2024-07-22
 */
@Service
public class DriverServiceImpl implements IDriverService {


    @Autowired
    private DriverMapper driverMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private RealTimeOrderRmote realTimeOrderRmote;

    /**
     * 查询司机
     *
     * @param driverId 司机主键
     * @return 司机
     */
    @Override
    public Driver selectDriverByDriverId(Long driverId) {
        return driverMapper.selectDriverByDriverId(driverId);
    }

    /**
     * 查询司机列表
     * @param driver 司机
     * @return 司机
     */
    @Override
    public List<Driver> selectDriverList(Driver driver) {
        return driverMapper.selectDriverList(driver);
    }

    /**
     * 新增司机
     *
     * @param driver 司机
     * @return 结果
     */
    @Override
    public int insertDriver(Driver driver) {
            return driverMapper.insertDriver(driver);
    }

    /**
     * 修改司机
     *
     * @param driver 司机
     * @return 结果
     */
    @Override
    public int updateDriver(Driver driver) {
        return driverMapper.updateDriver(driver);
    }

    /**
     * 批量删除司机
     *
     * @param driverIds 需要删除的司机主键
     * @return 结果
     */
    @Override
    public int deleteDriverByDriverIds(Long[] driverIds) {
        return driverMapper.deleteDriverByDriverIds(driverIds);
    }

    /**
     * 删除司机信息
     *
     * @param driverId 司机主键
     * @return 结果
     */
    @Override
    public int deleteDriverByDriverId(Long driverId) {
        return driverMapper.deleteDriverByDriverId(driverId);
    }





    @Override
    public Result updOrdersStatusDriver(RealTime realTime) {
        Result list = realTimeOrderRmote.list(realTime);
        RealTime realTimedata = (RealTime) list.getData();
        Integer ordersStatusDriver = driverMapper.updOrdersStatusDriver(realTimedata.getRealtimeId());
        if(ordersStatusDriver>0){
            sendEmail(realTime.getUserTel());
        }
        return Result.success();
    }




    private void sendEmail(String userTel) {
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setSubject("司机接单");
            mimeMessageHelper.setFrom(userTel + "@163.com");
            mimeMessageHelper.setTo(userTel);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }







}
